package com.example.protestgps;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;

import com.google.android.gms.maps.GoogleMap;

public class PostSvnEmer extends Activity  {
	
		
		JSONObject jsob;
		HTTPPOSTER1 postter1 = new HTTPPOSTER1();
		
		String lat;
		String lng;
		double latdo;
		double lngdo;
		double deslat;
		double deslng;
		GoogleMap Map;
		
		public void curpointemer(double lat,double lng,GoogleMap Map){
		
			this.lat = String.valueOf(lat);
			this.lng = String.valueOf(lng);
			this.latdo = lat;
			this.lngdo = lng;
			this.Map = Map;		
		}
		

	public class HTTPPOSTER1 extends AsyncTask<String,String, String> {

		JSONObject jObj;
	    String json = "";
	    String result = "";
	  


		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			parseJSON(result,latdo,lngdo);
		}

		
		@Override
		public String doInBackground(String... urls) {
			// TODO Auto-generated method stub
			String result = "";
		
			ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();
			data.add(new BasicNameValuePair("Lat", ""+lat));
			Log.d("lat stringh",""+lat);
			data.add(new BasicNameValuePair("Lng", ""+lng));
			
			
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new  HttpPost(urls[0]);
			Log.d("doin","doin");
			
			
				try {
					
					post.setEntity(new UrlEncodedFormEntity(data,HTTP.UTF_8));
					HttpResponse response = client.execute(post);
					HttpEntity entity = response.getEntity();
				
					if(entity != null){
						InputStream inputStream = entity.getContent();
						BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
						StringBuilder sb = new StringBuilder();
						String line = null;
						
						
						while((line = reader.readLine()) != null){
							sb.append(line + "\n");
						}
						
						
						inputStream.close();
						result = sb.toString();
														
					 }
					
					
				} catch (Exception e) {
					e.printStackTrace();
				}		
				
			
		
			Log.d("result1","result:"+result);
			Log.d("json1 ","json1 :"+json);
			//setjObj(jObj);
		
			
			//HttpPostSvr1(jObj);
			return result;
			
		 }

	   }


	private void parseJSON(String result,Double lat1,Double lng1) {
		try {
		JSONObject jObject = new JSONObject(result);
		JSONArray data = jObject.getJSONArray("data");
		String[] lat = new String[data.length()];
		String[] lng = new String[data.length()];
		for (int i = 0; i < data.length(); i++) {
			JSONObject c = data.getJSONObject(i);
			 lat[i] = c.getString("lat");
			 lng[i] = c.getString("lng");
			 Log.d("aa",""+lat[i]);
		}
		
		double datalat1 = Double.parseDouble(lat[0]);
		double datalat2 = Double.parseDouble(lat[1]);
		double datalat3 = Double.parseDouble(lat[3]);
		double datalat4 = Double.parseDouble(lat[4]);
		double datalat5 = Double.parseDouble(lat[5]);
		double datalng1 = Double.parseDouble(lng[0]);
		double datalng2 = Double.parseDouble(lng[1]);
		double datalng3 = Double.parseDouble(lng[3]);
		double datalng4 = Double.parseDouble(lng[4]);
		double datalng5 = Double.parseDouble(lng[5]);
		
		double _distToCurr1 = Math.sqrt(Math.pow((datalat1-lat1),2)+Math.pow(datalng1-lng1,2));
		double _distToCurr2 = Math.sqrt(Math.pow((datalat2-lat1),2)+Math.pow(datalng2-lng1,2));
		double _distToCurr3 = Math.sqrt(Math.pow((datalat3-lat1),2)+Math.pow(datalng3-lng1,2));
		double _distToCurr4 = Math.sqrt(Math.pow((datalat4-lat1),2)+Math.pow(datalng4-lng1,2));
		double _distToCurr5 = Math.sqrt(Math.pow((datalat5-lat1),2)+Math.pow(datalng5-lng1,2));
		
		Log.d("_distToCurr2",""+_distToCurr2);
	    
		if(_distToCurr1<_distToCurr2&&_distToCurr1<_distToCurr3&&_distToCurr1<_distToCurr4&&_distToCurr1<_distToCurr5){
			
		
			
			
			CountDown count= new CountDown();
			count.recdata1(datalat1, datalng1,latdo,lngdo,Map);
			
			
		}
		

	    if(_distToCurr2<_distToCurr1&&_distToCurr2<_distToCurr3&&_distToCurr2<_distToCurr4&&_distToCurr2<_distToCurr5){
			
	    
	    	CountDown count= new CountDown();
			count.recdata1(datalat2, datalng2,latdo,lngdo,Map);
		}
	    
	    if(_distToCurr3<_distToCurr1&&_distToCurr3<_distToCurr2&&_distToCurr3<_distToCurr4&&_distToCurr3<_distToCurr5){
			
	    
	    	
	    	CountDown count= new CountDown();
			count.recdata1(datalat3, datalng3,latdo,lngdo,Map);
			
	   	}
	    
	    if(_distToCurr4<_distToCurr1&&_distToCurr4<_distToCurr2&&_distToCurr4<_distToCurr3&&_distToCurr4<_distToCurr5){
			
	    
	    	CountDown count= new CountDown();
			count.recdata1(datalat4, datalng4,latdo,lngdo,Map);
	   	}
	    
	   if(_distToCurr5<_distToCurr1&&_distToCurr5<_distToCurr2&&_distToCurr5<_distToCurr3&&_distToCurr5<_distToCurr4){
			
		
		    CountDown count= new CountDown();
			count.recdata1(datalat4, datalng4,latdo,lngdo,Map);
	   	}
		
		
		
	   } catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	  } 
	 }
  }

